﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.圣同润.merchant;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form发货数据导入 : DevExpress.XtraEditors.XtraForm
    {
        private String brandName { get; set; }
        private int brandId;
        //  导入订单物流信息一览
        private List<OrderLogisticsInfoImport> orderLogistics = new List<OrderLogisticsInfoImport>();
        public Form发货数据导入()
        {
            InitializeComponent();
            this.searchLookUpEdit品牌商.Properties.DataSource = MerchantService.getBrandAll();
        }


        #region 选择品牌商
        private void searchLookUpEdit品牌商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear品牌商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit品牌商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit品牌商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String id = this.searchLookUpEdit品牌商.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.brandId = Convert.ToInt32(id);
                    this.brandName = this.searchLookUpEdit品牌商.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear品牌商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit品牌商.ToolTip = null;
            searchLookUpEdit品牌商.EditValue = null;
        }
        private void searchLookUpEdit品牌商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.brandName;
        }
        #endregion


        private void simpleButton发货数据导入_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.brandName))
            {
                MessageBox.Show("请先选择要导入数据对应的品牌商");
                return;
            }
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                DataSet result = ExcelUtil.getExcelDatas(openFile.FileName);
                if (result == null)
                {
                    return;
                }
                DataTable dataTable = result.Tables[0];

                this.orderLogistics.Clear();
                int i = 0;
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    if (i != 0)
                    {
                        OrderLogisticsInfoImport orderLogisticsInfoImport = new OrderLogisticsInfoImport(dataRow);
                        if (!String.IsNullOrEmpty(orderLogisticsInfoImport.delivery_id))
                            this.orderLogistics.Add(orderLogisticsInfoImport);
                    }

                    i++;
                }
                this.gridControlImportExcel.DataSource = this.orderLogistics;
                this.gridViewImportExcel.RefreshData();
                gridViewImportExcel.OptionsBehavior.AutoExpandAllGroups = true;
            }
            simpleButton提交发货数据.Enabled = true;
        }

        private void simpleButton提交发货数据_Click(object sender, EventArgs e)
        {
            if (this.orderLogistics.Count == 0)
            {
                MessageBox.Show("无物流信息上传");
                return;
            }
            List<LogisticsBody> logisticsBodies = new List<LogisticsBody>();
            foreach (var item in this.orderLogistics)
            {
                logisticsBodies.Add(item.build());
            }
            //  上传物流信息
            MerchantService.saveLogisticsBodies(this.brandId, this.brandName, logisticsBodies);
            MessageBox.Show("物流信息上传成功");
            //  刷新
            this.orderLogistics = new List<OrderLogisticsInfoImport>();
            this.gridControlImportExcel.DataSource = this.orderLogistics;
            this.gridViewImportExcel.RefreshData();
            gridViewImportExcel.OptionsBehavior.AutoExpandAllGroups = true;
            simpleButton提交发货数据.Enabled = false;
        }
    }
}